let Bkp = cc.Class({
    extends: cc.Component,

    properties: {},

    statics: {
        Type: {
            COLLISION: 1,
            HOLE: 2,
            BRIDGE: 3,
        }
    },

    // LIFE-CYCLE CALLBACKS:
    ctor() {
        this._itemModel = null;
        this._lstItem = [];
    },

    onLoad() {
        this.graphics = this.node.getComponent(cc.Graphics);
    },

    addItem(item){
        this._lstItem.push(item);
    },

    clean(){
        this._lstItem.length = 0;
        this.updateDatas();
    },

    updateDatas(){

        // 先绘制直线看看
        let graphics = this.graphics;
        graphics.clear();

        if(this._lstItem.length < 2){
            return;
        }
        let firstItem = this._lstItem[0];
        graphics.moveTo(firstItem.pos.x,firstItem.pos.y);


        // 间隔
        let len = this._lstItem.length;
        let idx_gap = settings.SC_IDX_GAP;
        let curIdx = idx_gap;
        while (curIdx < len - 1){
            let cur = this._lstItem[curIdx];
            let nextIdx = curIdx + idx_gap;
            if (nextIdx > len - 1) {
                nextIdx = len - 1;
            }
            graphics.lineTo(cur.pos.x,cur.pos.y);
            curIdx = nextIdx;
        }

        // 最后一个
        let lastItem = this._lstItem[len - 1];
        graphics.moveTo(lastItem.pos.x,lastItem.pos.y);
        graphics.stroke();
    },

    getItemModel() {
        return this._itemModel;
    },

    setItemModel(itemModel) {
        this._itemModel = itemModel;
    },

    setType(type) {
        this._type = type;
        switch (type) {
            case Bkp.Type.COLLISION:
                this.setStrokeColor(cc.Color.RED);
                this.node.zIndex = settings.Z_Collision;
                this.updateDatas();
                break;
            case Bkp.Type.HOLE:
                this.setStrokeColor(cc.Color.BLACK);
                this.node.zIndex = settings.Z_Hole;
                this.updateDatas();
                break;
            case Bkp.Type.BRIDGE:
                this.setStrokeColor(cc.Color.GREEN);
                this.node.zIndex = settings.Z_Bridge;
                this.updateDatas();
                break;
            default:
                settings.alert(`unknow bkp type:${type}`);
        }
    },

    setStrokeColor(color){
        this.graphics.strokeColor = color;
    },

    // start() {
    //
    // },

    // update (dt) {},
});

module.exports = Bkp;
